const Mock = require('mockjs')
const bodyParser = require('body-parser')
const JWT=require('jsonwebtoken')
const user=[{
    "username":"18510204378",
    "password":"666666",
},{
    "username":"13522506563",
    "password":"111111",
},{
    "username":"17621856309",
    "password":"222222",
}]
const {tablist}=Mock.mock({
    "tablist|8":[{
        "title":"@ctitle(3)",
        "children|8":[{
            "title":"@ctitle(2)",
            "img":"@image(100x100,@color)",
            "word":"@cword(8)"
        }]
    }]
})
let {list}=Mock.mock({
    "list|10":[{
        "img":"@image(100x100,@color)",
        "title":"@ctitle(4)",
        "word":"@cword(5)",
        "price|10-100":1,
        "id":"@id",
    }]
})
module.exports = function (app) {
    app.use(bodyParser.json())
    app.post('/api/login',(req,res)=>{
        let {username,password}=req.body
        let item=user.find((item)=>item.username===username && item.password===password)
        if (item) {
            res.send({
                code:200,
                msg:'登录成功',
                token:JWT.sign(item.username,"sdfghjkl")
            })
        }else{
            res.send({
                code:400,
                msg:"登录失败"
            })
        }
    })
    app.get('/api/tablist',(req,res)=>{
        res.send({
            code:200,
            tablist
        })
    })
    app.get('/api/list',(req,res)=>{
        res.send({
            code:200,
            list
        })
    })
    app.get('/api/search',(req,res)=>{
        res.send({
            code:200,
            list
        })
    })
    app.get('/api/del',(req,res)=>{
        const {id}=req.query
        list=list.filter((item)=>item.id!==id)
        res.send({
            code:200,
            msg:"删除成功"
        })
    })
    app.get('/api/detail',(req,res)=>{
        const {id}=req.query
        const item=list.find((item)=>item.id===id)
        if (item) {
            res.send({
                code:200,
                item
            })
        }
    })
}
